package com.dhcc.cdc.sys.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.sys.entity.DataTableEntity;
import com.dhcc.cdc.sys.entity.DictionaryEntity;
import com.dhcc.cdc.sys.service.IDataTableService;
import com.dhcc.cdc.sys.service.IDictionaryService;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


/**
 * 业务数据类型与表的关联关系
 * 
 * @ClassName: DataTableController
 * @Description: TODO
 * @author jumper
 * @date 2021-04-27 16:39:52
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/datatable")
public class DataTableController extends BaseController {

	private String PREFIX = "/backend/cdc/datatable/";
    
	@Autowired
	private IDataTableService dataTableService;
	@Autowired
    private IDictionaryService dictionaryService;

    /**
     * 跳转到业务数据类型与表的关联关系列表首页
     */
    @RequestMapping("")
    @Permission("datatable:list")
    public String index() {
        return PREFIX + "datatable.html";
    }
	
    /**
     * 跳转到新增业务数据类型与表的关联关系
     */
    @RequestMapping("/to_add")
    @Permission("datatable:add")
    public String toAdd() {
        return PREFIX + "datatable_add.html";
    }
	
	/**
     * 跳转到修改业务数据类型与表的关联关系
     */
    @RequestMapping("/to_update/{dataTableId}")
    @Permission("datatable:update")
    public String toUpdate(@PathVariable Long dataTableId, Model model) {
        DataTableEntity dataTable = this.dataTableService.selectById(dataTableId);
        model.addAttribute("dataTable",dataTable);
        return PREFIX + "datatable_edit.html";
    }

    /**
     * 获取业务数据类型与表的关联关系列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission("datatable:list")
    public Object list(@RequestParam Map<String, Object> params) {
		Page<DataTableEntity> page = new PageFactory<DataTableEntity>().defaultPage();
        List<DataTableEntity> result = this.dataTableService.page(page,params);
        page.setRecords((List<DataTableEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    @RequestMapping(value = "/query")
    @Permission("datatable:list")
    @ResponseBody
    public Object query(@RequestParam Map<String, Object> params) {
        List<DataTableEntity> result = this.dataTableService.list(params);
    	return result;
    }

    @RequestMapping(value = "/table")
    @Permission("datatable:list")
    @ResponseBody
    public Object table(@RequestParam Map<String, Object> params) {
        List<DataTableEntity> dataTable = this.dataTableService.list(params);
        String[][] tableCode= new String[2][dataTable.size()];
        dataTable.forEach(dt->{
            DictionaryEntity dictionary=this.dictionaryService.selectById(dt.getSysDictionaryId());
            String code=dictionary.getCode();
            String description=dictionary.getDescription();

            int index=dataTable.indexOf(dt);
            tableCode[0][index]=code;
            tableCode[1][index]=description;
        });
    	return tableCode;
    }
    /**
     * 新增业务数据类型与表的关联关系
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("datatable:add")
    @BussinessLog(value = "新增业务数据类型与表的关联关系")
    public Object add(DataTableEntity dataTable) {
        if (CommonUtil.isOneEmpty(dataTable)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        dataTable.insert();
        return R.ok();
    }

    /**
     * 删除业务数据类型与表的关联关系
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("datatable:delete")
    @BussinessLog(value = "删除业务数据类型与表的关联关系")
    public Object delete(@RequestParam Long dataTableId) {
	
        this.dataTableService.deleteById(dataTableId);

        return R.ok();
    }

    /**
     * 修改业务数据类型与表的关联关系
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("datatable:update")
    @BussinessLog(value = "修改业务数据类型与表的关联关系")
    public Object update(DataTableEntity dataTable) {
        if (CommonUtil.isOneEmpty(dataTable)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        DataTableEntity old = this.dataTableService.selectById(dataTable.getId());

        old.setSysDataId(dataTable.getSysDataId());
        old.setSysDictionaryId(dataTable.getSysDictionaryId());
        old.updateAllColumnById();
        return R.ok();
    }
	
}
